<?php
/**
 * 如花拼团开源系统 -- 永久免费
 * =========================================================
 * 官方网址：http://www.phps.shop
 * 作者：光爵【API + 后台】 、 小草【小程序 + WAP】
 * QQ 交流群：728615087
 * Version：1.0.0
 */

namespace app\api\model;


use app\lib\exception\BaseException;
use think\model\concern\SoftDelete;
use app\lib\exception\SuccessMessage;
use app\api\model\SchedulingDetail;
use think\Db;

class UserScheduling extends BaseModel
{
    use SoftDelete;
    protected $hidden = ['update_time','create_time'];
    protected $deleteTime = 'delete_time';
    protected $type = [
        'start_time'     =>  'timestamp:Y-m-d',
        'end_time'  =>  'timestamp:Y-m-d',
    ];

    //关联详情行程表
    public function detail(){
        return $this->hasone('SchedulingDetail','pid','id');
    }

    //关联详情行程表
    public function detailAll(){
        return $this->hasMany('SchedulingDetail','pid','id');
    }

    //创建用户行程安排
    public static function CreateOne($uid,$post)
    {
        $UserScheduling=new UserScheduling;
        Db::startTrans();
        try {
            $post['user_id'] = $uid;
            $res  = (empty($post['id'])) ? false : true;
            $UserScheduling->isUpdate($res)->save($post); //直接通过关联模型来新增
            Db::commit();
        }catch (\Exception $e) {
            Db::rollback();// 回滚事务
            throw new BaseException(['msg'=>'新增行程安排失败']);
        }
        return json(new SuccessMessage(), 201);
    }

    //删除用户行程安排
    public static function scheduling_del($id,$uid)
    {
        $res = UserScheduling::destroy(['id'=>$id,'user_id'=>$uid]);
        if(!$res){
            throw new BaseException(['删除错误']);
        }else{
            return json(new SuccessMessage(),201);
        }
    }

    //删除用户所有安排
    public static function scheduling_all_del($uid){
        $res = UserScheduling::where(['user_id'=>$uid])->field('id')->select();
        if(!$res->isEmpty()){
            Db::startTrans();
            try {
                foreach ($res->toArray() as $key => &$value)
                    $ids[] = $value['id'];
                UserScheduling::where('id','in',$ids)->delete();
                SchedulingDetail::where('pid','in',$ids)->delete();
                Db::commit();
            }catch (\Exception $e) {
                Db::rollback();// 回滚事务
                throw new BaseException(['msg'=>'批量删除错误']);
            }
        }
        return json(new SuccessMessage(),201);
    }

    //一键复制参考
    public static function scheduling_refer_add($id,$uid){
        $UserScheduling = new UserScheduling();
        $SchedulingDetail = new SchedulingDetail();
        $res = self::with(['detail_all'])->where(['id'=>$id])->find();
        $refer = $res->toArray();
        $refer['user_id'] = $uid;
        unset($refer['id']);
        unset($refer['detail_all']);
        Db::startTrans();
        try {
            $UserScheduling->save($refer);
            $pid = $UserScheduling->id;
            $array = ['name','stay','transport','scheduling','start_time','end_time'];
            foreach ($res['detail_all'] as $key => &$value){
                $refer2[$key]['pid'] = $pid;
                foreach ($array as $key2 =>$value2){
                    $refer2[$key][$value2] = $value[$value2];
                }
            }
            $SchedulingDetail->saveAll($refer2);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();// 回滚事务
            throw new BaseException(['msg' => $refer2]);
        }
        return json(new SuccessMessage(),201);
    }
}